﻿using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.BasicsDatum;
using SmartWMS.Domain.SystemSet;
using SmartWMS.Server.BasicsDatumServer.ProductManagement.Dto;
using SmartWMS.Server.BasicsDatumServer.SupplierManagement;
using SmartWMS.Server.Repository;
using SmartWMS.Server.SystemSets.MessageCenterService.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartWMS.Server.SystemSets.MessageCenterService
{
    /// <summary>
    /// 消息中心实现层
    /// </summary>
    public class MessageCenterService : IMessageCenterService
    {
        private readonly IRepository<MessageCenter> _messageCenter;

        public MessageCenterService(IRepository<MessageCenter> messageCenter)
        {
            _messageCenter = messageCenter;
        }

        /// <summary>
        /// 消息中心反填
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Response<MessageCenterDto> GetSupplierManagementById(MessageCenterDto dto)
        {
            //反填
            var info = _messageCenter.GetListQuery().FirstOrDefault(m => m.Id == dto.Id);
            var result = info.MapTo<MessageCenterDto>();
            return Response<MessageCenterDto>.Ok(result);
        }

        /// <summary>
        /// 消息中心列表
        /// </summary>
        /// <param name="messageCenterSelectDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Response<dynamic> MessageCenterGetList(MessageCenterSelectDto messageCenterSelectDto)
        {
            var list = _messageCenter.GetListQuery();

            if (string.IsNullOrEmpty(messageCenterSelectDto.MsgType)==false)
            {
                list = list.Where(x => x.MsgType== messageCenterSelectDto.MsgType);
            }
            //分页
            int totalCount = list.Count();
            int pageCount = (int)Math.Ceiling(totalCount * 1.0 / messageCenterSelectDto.pageSize);
            list = list.Skip((messageCenterSelectDto.pageIndex - 1) * messageCenterSelectDto.pageSize).Take(messageCenterSelectDto.pageSize);


            var result = list.MapToList<MessageCenterDto>();
            return Response<dynamic>.Ok(new
            {
                totalCount,
                pageCount,
                result
            });
        }
    }
}
